/**
  ******************************************************************************
  * @file    User/user.c
  * @author  JUSHENG Application Team
  * @version V1.0.4
  * @date    12-14-2021
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2021 JUSHENG</center></h2>
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"
#include "user.h"
#include "tk_cfg.h"

/** @addtogroup Template_Project
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void timer_cap_init(void)
{
    TYPE_LL_GPIO_INIT gpio_struct;
    TYPE_LL_TIMER_INIT p_init;
    TYPE_LL_TIMER_CAP_CFG cap_cfg;

    p_init.prescaler = LL_TIMER_PSC_NONE;
    p_init.timer_src_sel = LL_TIMER_SRC_SYS_RISING;
    ll_timer_init(TIMER4, &p_init);

    memset(&gpio_struct, 0x0, sizeof(gpio_struct));
    gpio_struct.gpio_pin   = LL_GPIO_PIN_7;
    gpio_struct.gpio_mode  = LL_GPIO_MODE_AF;
    gpio_struct.gpio_pupd  = LL_GPIO_PUPD_UP;
    ll_gpio_init(GPIOB,&gpio_struct);
    ll_gpio_pin_af_config(GPIOB, LL_GPIO_PIN_SOURCE_7, LL_GPIO_AF_1);

    cap_cfg.cap_mode = LL_TIMER_CAP_MODE_CAP1;
    cap_cfg.cap1_pol_sel = LL_TIMER_CAP_POL_FALLING; 
    cap_cfg.cap1_ie = ENABLE;
    ll_timer_cap_mode_config(TIMER4, &cap_cfg); 
    ll_timer_start(TIMER4, LL_TIMER_MODE_SEL_CAPTURE);

    NVIC_InitTypeDef nvic; 
    nvic.NVIC_IRQChannel = TIM4_IRQn;
    nvic.NVIC_IRQChannelPriority = 0;
    nvic.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic);

}

void gpio_init(void)
{
    TYPE_LL_GPIO_INIT gpio_struct;

    memset(&gpio_struct,0x00,sizeof(gpio_struct));
    gpio_struct.gpio_pin   = LL_GPIO_PIN_0;
    gpio_struct.gpio_mode  = LL_GPIO_MODE_OUT;
    ll_gpio_init(GPIOA, &gpio_struct);
}

void timer_cap_handle(void)
{
    ll_gpio_set_bits(GPIOA, LL_GPIO_PIN_0);
    delay_ms(1);
    ll_gpio_reset_bits(GPIOA, LL_GPIO_PIN_0);
    delay_ms(1);
    ll_gpio_set_bits(GPIOA, LL_GPIO_PIN_0);
    delay_ms(1);
    ll_gpio_reset_bits(GPIOA, LL_GPIO_PIN_0);
    delay_ms(1);
    ll_gpio_set_bits(GPIOA, LL_GPIO_PIN_0);
    delay_ms(1);
}

/**
  * @}
  */

/*************************** (C) COPYRIGHT 2021 JUSHENG ***** END OF FILE *****/
